package com.service;


import com.github.binarywang.wxpay.bean.notify.WxPayNotifyResponse;
import com.github.binarywang.wxpay.bean.notify.WxPayOrderNotifyResult;
import com.github.binarywang.wxpay.bean.order.WxPayAppOrderResult;
import com.github.binarywang.wxpay.bean.request.WxPayUnifiedOrderRequest;
import com.github.binarywang.wxpay.constant.WxPayConstants;
import com.github.binarywang.wxpay.exception.WxPayException;
import com.github.binarywang.wxpay.service.WxPayService;
import com.util.IPUtil;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;

/**
 * @author jiavDad
 */
@Service
@Slf4j
public class WxAppPayService {
    @Autowired
    private WxPayService wxService;

    @Value("${wxPay.callbackPath}")
    private String wxPayPath;

    @Autowired
    private HttpServletRequest request;

    public WxPayAppOrderResult createDefault(String tradeNo, BigDecimal price, Integer type, String body, Long userId) {
        WxPayUnifiedOrderRequest orderRequest = new WxPayUnifiedOrderRequest();
        //签名类型
        orderRequest.setSignType(WxPayConstants.SignType.MD5);
        //终端IP
        orderRequest.setSpbillCreateIp(IPUtil.getIpAddr(request));
        //商品描述 例如： 腾讯充值中心-QQ会员充值
        orderRequest.setBody(body);
        //商户订单号 商户系统内部的订单号,32个字符内、可包含字母
        orderRequest.setOutTradeNo(tradeNo);
        //回调地址
//        orderRequest.setNotifyUrl(wxPayPath + tradeNo + "/" + type);
//      Integer type = memberRewardByUserReq.getType().equals(1) ? IWxPayService.Type.REWARDPOST : IWxPayService.Type.REWARDCOMMENT;
        orderRequest.setNotifyUrl(wxPayPath + tradeNo + "/" + type + "/" + userId);

        //支付类型
        orderRequest.setTradeType("APP"); //如果你不是app支付就填其他的
//        orderRequest.setOpenid(openId); //app支付不需要openId
        orderRequest.setTotalFee(price.multiply(new BigDecimal(100)).intValue());
        log.error("支付请求参数:[{}]", orderRequest);
        try {
            log.error("生成订单参数：[{}]", orderRequest);
            WxPayAppOrderResult wxPayAppOrderResult = wxService.createOrder(orderRequest);
            return wxPayAppOrderResult;
        } catch (WxPayException e) {
            log.error("微信支付失败！原因：{}", e.getMessage());
        }
        throw new RuntimeException("微信支付失败！");
    }

    @SneakyThrows
    //@Transactional(rollbackFor = Exception.class)
    public String parseOrderNotifyResult(String xmlData) {
        log.info("手机微信支付回调,xml:[{}],tradeNo:[{}],type:[{}],userId:[{}]", xmlData);
        WxPayOrderNotifyResult notifyResult = wxService.parseOrderNotifyResult(xmlData);
        notifyResult.checkResult(wxService, "MD5", true);
        String openid = notifyResult.getOpenid();
        String transactionId = notifyResult.getTransactionId();


        return WxPayNotifyResponse.success("成功");
    }

}


